// HeeksCAE.cpp
// Copyright (c) 2009, Dan Heeks
// This program is released under the BSD license. See the file COPYING for details.
//

#include "stdafx.h"

#include <wx/stdpaths.h>
#include <wx/dynlib.h>
#include <wx/aui/aui.h>
#include "interface/PropertyString.h"
#include "interface/PropertyList.h"
#include "interface/PropertyInt.h"
#include "interface/Observer.h"
#include "interface/ToolImage.h"
#include "CAEConfig.h"
#include "Force.h"

CHeeksCADInterface* heeksCAD = NULL;

CHeeksCAEApp theApp;

CHeeksCAEApp::CHeeksCAEApp(){
	m_forceBar = NULL;
	m_num_facets_on_edge = 8;
}

CHeeksCAEApp::~CHeeksCAEApp(){
}

void CHeeksCAEApp::OnInitDLL()
{
}

void CHeeksCAEApp::OnDestroyDLL()
{
#if !defined WXUSINGDLL
	wxUninitialize();
#endif

	heeksCAD = NULL;
}

void OnForceBar( wxCommandEvent& event )
{
	wxAuiManager* aui_manager = heeksCAD->GetAuiManager();
	wxAuiPaneInfo& pane_info = aui_manager->GetPane(theApp.m_forceBar);
	if(pane_info.IsOk()){
		pane_info.Show(event.IsChecked());
		aui_manager->Update();
	}
}

void OnUpdateForceBar( wxUpdateUIEvent& event )
{
	wxAuiManager* aui_manager = heeksCAD->GetAuiManager();
	event.Check(aui_manager->GetPane(theApp.m_forceBar).IsShown());
}

static void OnAddForce(wxCommandEvent& event)
{
	CForce* force = new CForce(0, 0, 0, 0, 0, 1);
	heeksCAD->Add(force, NULL);
	heeksCAD->Changed();
}

static void AddToolBars()
{
	wxFrame* frame = heeksCAD->GetMainFrame();
	wxAuiManager* aui_manager = heeksCAD->GetAuiManager();
	if(theApp.m_forceBar)delete theApp.m_forceBar;
	theApp.m_forceBar = new wxToolBar(frame, -1, wxDefaultPosition, wxDefaultSize, wxTB_NODIVIDER | wxTB_FLAT);
	theApp.m_forceBar->SetToolBitmapSize(wxSize(ToolImage::GetBitmapSize(), ToolImage::GetBitmapSize()));
	heeksCAD->AddToolBarButton((wxToolBar*)(theApp.m_forceBar), _("Add a force"), ToolImage(_T("force")), _T("Add a force"), OnAddForce);
	theApp.m_forceBar->Realize();
	aui_manager->AddPane(theApp.m_forceBar, wxAuiPaneInfo().Name(_T("ForceBar")).Caption(_T("Force tools")).ToolbarPane().Top());
	heeksCAD->RegisterToolBar(theApp.m_forceBar);
}

void CHeeksCAEApp::OnStartUp(CHeeksCADInterface* h, const wxString& dll_path)
{
	m_dll_path = dll_path;
	heeksCAD = h;
#if !defined WXUSINGDLL
	wxInitialize();
#endif

	CAEConfig config;

	// About box, stuff
	heeksCAD->AddToAboutBox(wxString(_T("\n\n")) + _("HeeksCAE is a free Finite Element Analysis add-on to HeeksCAD")
		+ _T("\n") + _("          http://code.google.com/p/heekscae/")
		+ _T("\n") + _("Written by Dan Heeks")
		);

	// add menus and toolbars
	wxFrame* frame = heeksCAD->GetMainFrame();

	// tool bars
	heeksCAD->RegisterAddToolBars(AddToolBars);
	AddToolBars();

	// Force menu
	wxMenu *menuForce = new wxMenu;
	heeksCAD->AddMenuItem(menuForce, _("Add a force"), ToolImage(_T("force")), OnAddForce);
	frame->GetMenuBar()->Append(menuForce,  _("Force"));

	// add tick boxes for them all on the Window menu
	wxMenu* window_menu = heeksCAD->GetWindowMenu();
	heeksCAD->AddMenuItem(window_menu, _T("Forces"), wxBitmap(), OnForceBar, OnUpdateForceBar, NULL, true);
	heeksCAD->RegisterHideableWindow(m_forceBar);

	// add object reading functions
	heeksCAD->RegisterReadXMLfunction("force", CForce::ReadFromXMLElement);
}

void CHeeksCAEApp::OnNewOrOpen(bool open)
{
}

static void on_set_some_value(int value, HeeksObj* object)
{
	//  theApp.m_some_value = value;
}

void CHeeksCAEApp::GetOptions(std::list<Property *> *list){
	PropertyList* finite_element_analysis_options = new PropertyList(_("finite element analysis options"));

	finite_element_analysis_options->m_list.push_back ( new PropertyInt ( _("some value"), /*theApp.m_some_value*/ 42, NULL, on_set_some_value ) );

	list->push_back(finite_element_analysis_options);
}

void CHeeksCAEApp::OnFrameDelete()
{
}

const wxString& CHeeksCAEApp::GetDllFolder()
{
	return m_dll_path;
}

class MyApp : public wxApp
{
 
 public:
 
   virtual bool OnInit(void);
 
 };
 
 bool MyApp::OnInit(void)
 
 {
 
   return true;
 
 }

 DECLARE_APP(MyApp)
 
 IMPLEMENT_APP(MyApp)
 
